angular.module('crawlerApp')
.controller('projectSetCtr', ['$scope', function($scope){
  $scope.controllerName = 'projectSetCtr';
  $scope.title = '项目配置';
  $scope.project ={ 
      "previous":"1",

      "name":"project1",

      "depth":-1,

      "start_url":"https://segmentfault.com/a/1190000002691861",

      "rule_store":[
        {
          "url_pattern":["https://segmentfault.com.*?"],

          "link_parse_rule":[
            {
              "mode":"reg",
              "expression":"href=\"(https://github.com/totorojs/totoro)\""
            }],

          "content_parse_rule":{},

          "request_options":{},

          "saveAddress":"/Users/lawwy/Work/project/wf-spider/lib/downloader/html/"
        }],

      'arguments':{
        'username':'username',
        'password':'password'
      }
  }

  $scope.templates = [{
      'name': '模板1'
    }, {
      'name': '模板2'
    }, {
      'name': '模板3'
    }, {
      'name': '模板4'
    }, {
      'name': '模板5'
    }
  ]
  $scope.moveUp = function(template) {
    angular.forEach($scope.project.templates, function(tpl, key) {
      if (angular.equals(tpl, template)) {
        if (key == 0) {
          return ;
        }else{
          $scope.project.templates.splice(key, 1)
          $scope.project.templates.splice(key-1, 0,template)
        }
        // $scope.project.templates.splice(key, 1)
      };
    });
    // alert('moveUp')
  }
  $scope.moveDown = function(template) {
    angular.forEach($scope.project.templates, function(tpl, key) {
      if (angular.equals(tpl, template)) {
        if (key == $scope.project.templates.length-1) {
          return ;
        }else{
          $scope.project.templates.splice(key, 1)
          $scope.project.templates.splice(key+1, 0,template)
        }
        // $scope.project.templates.splice(key, 1)
      };
    });
    // alert('moveDown')
  }
  $scope.showTemplate = function(template) {
    alert('showTemplate')
  }
  $scope.deleteTemplate = function(template) {
    angular.forEach($scope.project.templates, function(tpl, key) {
      if (angular.equals(tpl, template)) {
        $scope.project.templates.splice(key, 1)
      };
    });
    alert('deleteTemplate')
  }
  $scope.projectSetSave = function(task) {
    alert('projectSetSave')
  }
}])